import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/controllers/my_commodity_controller/my_commodity_page_controller.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:xinxuan/utils/total_util.dart';
import 'package:xinxuan/vo/common_vo/common_seckill_detail_data_vo.dart';
import 'package:xinxuan/vo/displacement_list_vo.dart';
import 'package:xinxuan/vo/entrust_vo.dart';
import 'package:xinxuan/vo/seckill_holder_products_vo.dart';
import 'package:xinxuan/vo/seckill_substitute_prices_vo.dart';

class ReplacementShelfPageController extends GetxController {

  final MyCommodityPageController myCommodityPageController = Get.find();
  
  /// 置换列表
  var goodsArr = <CommonSeckillDetailDataVo>[].obs;
  /// 是否同意置换协议
  var consentAgreement = true.obs;
  /// 差价
  var priceDifference = '0.00'.obs;
  /// 选择置换的商品
  var productId = 0.obs;
  /// 选择置换商品的价格
  var productPriceStr = '0.00'.obs;
  /// 置换商品详情
  var seckillHolderProductsData = SeckillHolderProductsData(
    title: '',
    image: '',
    price: '0.00',
    gift: Gift(
      title: '',
      quantity: 0
    )
  ).obs;
  /// 当前被置换的商品ID
  int thisProductId = 0;
  /// 当前被置换的HolderId
  int thisHolderId = 0;

  @override
  void onReady() {
    Map<String, dynamic> argumentsMap = Get.arguments;
    thisProductId = argumentsMap['productId'];
    thisHolderId = argumentsMap['holderId'];
    getDisplacementList(thisProductId);
    getSeckillHolderProductsDetail(thisHolderId);
    super.onReady();
  }

  /// 获取置换列表
  getDisplacementList(int thisProductId) async {
    DisplacementListVo displacementListVo = await HttpUtilService.getDisplacementList(productId: thisProductId);
    goodsArr.value = displacementListVo.data!;
    
    /// 差价
    priceDifference.value = '0.00';
    /// 选择置换的商品
    productId.value = 0;
    /// 选择置换商品的价格
    productPriceStr.value = '0.00';
    
    update();
  }

  /// 计算差价
  calculationPriceDifference(int holderProductId, int substituteProductId, String price) async {
    productId.value = substituteProductId;
    SeckillSubstitutePricesVo seckillSubstitutePricesVo = await HttpUtilService.getSeckillSubstitutePrices(holderProductId: holderProductId, substituteProductId: substituteProductId);
    priceDifference.value = seckillSubstitutePricesVo.data!.differencePrice ?? '0.00';
    productPriceStr.value = price;
    update();
  }

  /// 确认委托
  onfirmEntrust(int holderId) async {
    if (productId <= 0) {
      EasyLoading.showToast('请选择要置换的商品');
      return;
    }
    if (!consentAgreement.value) {
      EasyLoading.showToast('请阅读并同意置换协议');
      return;
    }
    EntrustVo? entrustVo = await HttpUtilService.onfirmEntrust(productId: productId.value, holderId: holderId);
    if (entrustVo != null) {
      Get.toNamed(
        '${AppRouters.COMMON_PAGE}${AppRouters.SECKILL_ORDER_PAY_PAGE}',
        arguments: {
          'payAmount': entrustVo.data!.paymentPrice!,
          'orderSn': entrustVo.data!.orderSn
        }
      )!.then((res) {
        if (res != null && res) {
          Future.delayed(Duration(milliseconds: 1000), () {
            myCommodityPageController.getMyCommodityList(loading: false);
          });
          Get.back(result: true);
          TotalUtil.showSuccess('置换成功');
        }
      });
    }
  }

  /// 获取置换商品详情
  getSeckillHolderProductsDetail(int productId) async {
    SeckillHolderProductsVo seckillHolderProductsVo = await HttpUtilService.getSeckillHolderProductsDetail(productId: productId);
    seckillHolderProductsData.value = seckillHolderProductsVo.data!;
    update();
  }

}